function [draw_data, num_draw] = image_drawread(DRAW_PATH, draw_type);

% DRAWREAD - obtains drawings from AIF drawings text file
%
%    [SDRAW, NDRAW] = DRAWREAD opens a text file specified by the user
%    and reads all drawings saved by AIW to this file.  The drawings
%    structure and number of drawings are stored to SDRAW and NDRAW.
%
%    [...] = DRAWREAD('ROI') only saves ROI drawings from the file.
%
%    [...] = DRAWREAD('ZONE') only saves zone drawings from the file.
%
%    [...] = DRAWREAD('PROFILE') only saves profile drawings from the
%    file.
%
%    NOTE:  Multiple drawing types may be specified in the input, i.e.
%           [...] = DRAWREAD('PROFILE ZONE') will save profiles & zones.

% By:   S.C. Molitor (smolitor@med.unc.edu)
% Date: September 23, 1999
%
% Modified:
% By:		Paul B. Manis (pmanis@med.unc.edu)
% Date:	16 March 2001. - made operational as a stand alone program...
%
% default parameters

file_filter = [DRAW_PATH];		% default file filter extension

% initialize outputs

draw_data = [];
num_draw = 0;

% check input arguments

if (nargin > 2)
   return
elseif (nargin == 0)
   draw_type = 'ALL';
elseif (~ischar(draw_type))
   return
end


% open drawings file
% exit if error

[p f e] = fileparts(file_filter);
if(isempty(f))
   [draw_name, temp_path] = uigetfile([file_filter '*.txt'], 'Drawings File');
   if ((draw_name == 0) & (temp_path == 0))
      return
   else
      DRAW_PATH = temp_path;
   end
else
   DRAW_PATH = p;
   if(DRAW_PATH(end) ~= '\')
      DRAW_PATH = [DRAW_PATH '\'];
   end;
   if(isempty(e))
      draw_name = [f '.txt'];
   else
      draw_name = [f e];
   end;
   
end;
draw_fid = fopen([DRAW_PATH draw_name], 'rt');
if (draw_fid == -1)
   fprintf(1, 'Cannot open %s', [DRAW_PATH draw_name]);
   return
end

% loop through file to read drawings

while (1)
   draw_text = fgetl(draw_fid);
   
   % last drawing or EOF
   
   if (draw_text == -1)
      break
   elseif (~isempty(findstr(draw_text, '[LastDrawing]')))
      break
      
      % increment drawing number
      
   elseif (~isempty(findstr(draw_text, '[Drawing')))
      num_draw = num_draw + 1;
      draw_data(num_draw).number = sscanf(draw_text, '[Drawing%d]');
      
      % drawing shape (BOX, ELLIPSE, etc)
      % AIW reverses POLYGON & POLYLINE
      
   elseif (~isempty(findstr(draw_text, 'Type=')))
      if (~isempty(findstr(draw_text, 'BOX')))
         draw_data(num_draw).shape = 'BOX';
      elseif (~isempty(findstr(draw_text, 'ELLIPSE')))
         draw_data(num_draw).shape = 'ELLIPSE';
      elseif (~isempty(findstr(draw_text, 'POLYGON')))
         draw_data(num_draw).shape = 'POLYLINE';
      elseif (~isempty(findstr(draw_text, 'POLYLINE')))
         draw_data(num_draw).shape = 'POLYGON';
      end
      
      % drawing type (ROI, ZONE, PROFILE)
      
   elseif (~isempty(findstr(draw_text, 'Properties=')))
      if (~isempty(findstr(draw_text, 'ROI')))
         draw_data(num_draw).type = 'ROI';
      elseif (~isempty(findstr(draw_text, 'ZONE')))
         draw_data(num_draw).type = 'ZONE';
      elseif (~isempty(findstr(draw_text, 'PROFILE')))
         draw_data(num_draw).type = 'PROFILE';
      end
      
      % drawing coordinates   
      % separate coordinate string into (X, Y) pairs
      % subtract image offset from coordinates
      
   elseif (~isempty(findstr(draw_text, 'Data=')))
      i_beg = min(findstr(draw_text, 'Data=')) + length('Data=');
      coord_str = strrep(draw_text(i_beg : end), ',', ' ');
      A = sscanf(coord_str, '%d');
      num_coord = 0;
      for i = 1 : 2 : length(A)
         num_coord = num_coord + 1;
         draw_data(num_draw).x(num_coord) = A(i);
         draw_data(num_draw).y(num_coord) = A(i + 1);
      end
   end
end

% delete unspecified drawings if needed

if (~strcmp(draw_type, 'ALL'))
   num_save = 0;
   for i = 1 : num_draw
      if (~isempty(findstr(draw_type, draw_data(i).type)))
         num_save = num_save + 1;
         draw_data(num_save) = draw_data(i);
      end
   end
   if (num_save < num_draw)
      draw_data(num_save + 1 : num_draw) = [];
      num_draw = num_save;
   end
end

% shift drawings by image offset if image data is present
% otherwise shift drawings to upper left corner

%x_shift = eval('AIF_IMAGE.info.left', 'min([draw_data(:).x])');
%y_shift = eval('AIF_IMAGE.info.top', 'min([draw_data(:).y])');
x_shift = 0;
y_shift = 0;
for i = 1 : num_draw
   draw_data(i).x = draw_data(i).x - x_shift + 1;
   draw_data(i).y = draw_data(i).y - y_shift + 1;
end

% close file & return

status = fclose(draw_fid);
return
